package org.krivtsun.scanlab.examples;

import javax.imageio.ImageIO;
import javax.sql.DataSource;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Enumeration;

/**
 * Created by IntelliJ IDEA.
 * User: andreyk
 * Date: 10.05.2007
 * Time: 20:23:05
 * To change this template use File | Settings | File Templates.
 */
public class Histogram {
    private static final String IMG_FILE_NAME = "d:\\che.tif";
    private static final String LOG_FILE_NAME = "d:\\res.txt";
    private static final int SUSCEPTIBILITY_THRESHOLD = 200;

    public static void main(String... args) {
        try {
            PrintWriter pw = new PrintWriter(new File(LOG_FILE_NAME));
            BufferedImage pageImage = ImageIO.read(new File(IMG_FILE_NAME));
            System.out.println("Image type " + pageImage.getType());
            long t = System.currentTimeMillis();
            for (int x = 0, xCount = pageImage.getWidth(); x < xCount; x++) {
                int count = 0;
                for (int y = 0, yCount = pageImage.getHeight(); y < yCount; y++) {
                    int rgbs = pageImage.getRGB(x, y);
                    int R = (rgbs >> 16) & 0xff;
                    int G = (rgbs >> 8) & 0xff;
                    int B = (rgbs) & 0xff;
                    int gray = (int) Math.round(0.3 * R + 0.59 * G + 0.11 * B);
                    if (gray < SUSCEPTIBILITY_THRESHOLD)
                        count++;
                }
                pw.println(count);
            }
            System.out.println("Processing time " + (System.currentTimeMillis() - t) + "ms");
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
